import java.io.*;
import java.sql.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class XML_Generator {

	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	static final String DB_URL = "jdbc:mysql://127.0.0.255:3306/iLink";
	//  Database credantials
	static final String USER = "root";
	static final String PASS = "";

    XML_Generator(){
    	send();
    }
    
    public void send(){
    	Connection con = null;
		try
		{
			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
			//creating a new instance of a DOM to build a DOM tree.
			Document doc = docBuilder.newDocument();
			createXmlTree(doc);

			//System.out.println("<b>Xml File Created Successfully</b>");
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
    }
	


	public void createXmlTree(Document doc) throws Exception {
		//This method creates an element node
		Element root = doc.createElement("File");
		//adding a node after the last child node of the specified node.
		doc.appendChild(root);

		Connection con = null;
		int count = 0;
		try{
			//STEP 2: Register JDBC driver
			Class.forName("com.mysql.jdbc.Driver");

			//STEP 3: Open a connection
			System.out.println("Connecting to database...");
			con = DriverManager.getConnection(DB_URL,USER,PASS);

			String sql = "SELECT * FROM files";
			PreparedStatement prest = con.prepareStatement(sql);
			ResultSet rs = prest.executeQuery();

			Element element=null;
			Text text1=null;
			while (rs.next()){
				count++;
				Element child = doc.createElement("Details");
				root.appendChild(child);

				Element child1 = doc.createElement("Name");
				child.appendChild(child1);

				Text text = doc.createTextNode(rs.getString(1));
				child1.appendChild(text);

				Element child2 = doc.createElement("location");
				child.appendChild(child2);

				text = doc.createTextNode(rs.getString(2));
				child2.appendChild(text);

				Element child3 = doc.createElement("size");
				child.appendChild(child3);

				text = doc.createTextNode(String.valueOf(rs.getLong(3)));
				child3.appendChild(text);

				Element child4 = doc.createElement("date");
				child.appendChild(child4);

				text = doc.createTextNode(rs.getString(4));
				child4.appendChild(text);

				Element child5 = doc.createElement("source");
				child.appendChild(child5);

				text = doc.createTextNode(rs.getString(5));
				child5.appendChild(text);

				//write the content into xml file
			}
			prest.close();
			con.close();
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);

			StreamResult result =  new StreamResult(new File("structure.xml"));
			transformer.transform(source, result);

		}
		catch (SQLException s){
			s.printStackTrace();
			System.out.println("SQL statement is not executed!");
		}
	}
}




